package com.yzt.dynamicmodule.activity;

import android.annotation.SuppressLint;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yzt.zhuju.arouter.RouterDynamic;
import com.yzt.zhuju.event.user.RefreshDynamicEvent;
import com.yzt.zhuju.mvp.MVPFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.game.GameBaseBean;
import com.yzt.zhuju.network.entity.game.GameListBannerBean;
import com.yzt.zhuju.network.entity.game.GameListBean;
import com.yzt.zhuju.photoview.PhotoViewManager;
import com.yzt.zhuju.widget.PageIndicatorView;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.dynamicmodule.R;
import com.yzt.dynamicmodule.adapter.ActivityListAdapter;
import com.yzt.dynamicmodule.banner.BannerViewHolder;
import com.yzt.dynamicmodule.presenter.ActivityListPresenter;
import com.yzt.p_base.AppUtils;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 活动列表
 */
@SuppressLint("SetTextI18n")
@Route(path = RouterDynamic.FRAGMENT_GAME_LIST)
public class GameListFragment extends MVPFragment<ActivityListPresenter>
        implements OnRefreshListener {

    /**
     * 轮播图
     */
    private MZBannerView<String> bannerView;
    private PageIndicatorView indicatorView;

    private PhotoViewManager photoViewManager;

    private final List<String> albums = new ArrayList<>();

    private ActivityListAdapter adapter;

    private RefreshLayout mRefreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_activity_list;
    }

    @Override
    public void installViews() {

        photoViewManager = PhotoViewManager.getInstance(AppUtils.getApp());

        // 刷新列表
        observerRxBus(RefreshDynamicEvent.class, new Action1<RefreshDynamicEvent>() {
            @Override
            public void call(RefreshDynamicEvent walletEvent) {
                mRefreshLayout.autoRefresh();
            }
        });

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ActivityListAdapter();
        recycle.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter1, view, position) -> {
            RouterDynamic.GameDetailActivity(adapter.getItem(position).getActivityId() + "");
        });

        adapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        // 初始轮播图
        initBannerView();

        mRefreshLayout.autoRefresh();
    }

    /**
     * banner栏
     */
    private void initBannerView() {
        indicatorView = findViewById(R.id.indicator);
        bannerView = findViewById(R.id.banner_view);
        bannerView.setIndicatorRes(R.drawable.shape_indicator_selected, R.drawable.shape_indicator_normal);
        bannerView.setIndicatorAlign(MZBannerView.IndicatorAlign.CENTER);
        bannerView.setBannerPageClickListener((view, position) -> {
            if (albums == null || albums.size() == 0) {
                return;
            }
            List<String> list = new ArrayList<>();
            List<String> newList = new ArrayList<>(albums);
            for (String album : newList) {
                list.add(ImageLoadUtil.url2Webp(album));
            }
            newList.clear();
            photoViewManager.showPhotoView(view, list, position);
        });

        bannerView.addPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.setPersonalSelectedPage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 加载相册
     */
    private void initAlbum() {
        indicatorView.setVisibility(albums.size() > 1 ? View.VISIBLE : View.GONE);
        indicatorView.initPersonalIndicator(albums.size());
        bannerView.setIndicatorVisible(false);
        bannerView.setPages(albums, (MZHolderCreator<BannerViewHolder>) BannerViewHolder::new);
        bannerView.setCanLoop(false);
    }

    @Override
    public void netWorkRequest() {

    }

    /**
     * 获取动态列表
     */
    public void getHttpData() {

        BizLayer.getInstance().getDynamicModule().activityList(mCurrentPage, PAGE_SIZE, new AresSubscriber<GameBaseBean>() {
            @Override
            protected void onSuccess(GameBaseBean bean) {

                albums.clear();

                for (GameListBannerBean banner : bean.getBanners()) {
                    albums.add(banner.getBannerImgUrl());
                }
                if (!albums.isEmpty()) {
                    findViewById(R.id.fl_banner).setVisibility(View.VISIBLE);
                    initAlbum();
                }

                List<GameListBean> list = bean.getList();
                if (mCurrentPage == 1) {
                    if (list.size() == 0) {
                        adapter.setEmptyView(getEmptyView());
                    } else {
                        adapter.removeEmptyView();
                    }
                    adapter.setList(list);
                } else {
                    adapter.addData(list);
                }
                adapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    adapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    adapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }
}